package com.dandelion.api.dao;

import com.rockit.core.pojo.Pagination;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;


public class UserAddressDaoSqlProvider {
    public String getUserAddressListSql(@Param("userId") long userId, Pagination pagination) {
        SQL sql = new SQL()
                .SELECT("t0.id as id")
                .SELECT("t0.user_id as userId")
                .SELECT("t0.prov_code as provCode")
                .SELECT("t0.city_code as cityCode")
                .SELECT("t0.area_code as areaCode")
                .SELECT("t0.district as district")
                .SELECT("t0.mobile as mobile")
                .SELECT("t0.name as name")
                .SELECT("t0.is_default as isDefault")
                .SELECT("t1.name as provName")
                .SELECT("t2.name as cityName")
                .SELECT("t3.name as areaName")
                .FROM("t_user_address t0")
                .LEFT_OUTER_JOIN("t_address_tree t1 on t1.code=t0.prov_code")
                .LEFT_OUTER_JOIN("t_address_tree t2 on t2.code=t0.city_code")
                .LEFT_OUTER_JOIN("t_address_tree t3 on t3.code=t0.area_code")
                .WHERE("t0.is_deleted=0 and t0.user_id=#{userId}")
                .ORDER_BY("t0.update_time desc");

        //limit
        StringBuilder buf = new StringBuilder(512);
        sql.usingAppender(buf);
        buf.append(" limit ").append(pagination.getOffset()).append(",").append(pagination.getLimit());

        return buf.toString();
    }
}
